package com.zjc.yohoshop.mvp.view.fragment;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.makeramen.roundedimageview.RoundedImageView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.AddCarEntity;
import com.zjc.yohoshop.bean.CollectionEntity;
import com.zjc.yohoshop.bean.GoodsValuesEntity;
import com.zjc.yohoshop.bean.SelGoodsValuesEntity;
import com.zjc.yohoshop.bean.UncollectionEntity;
import com.zjc.yohoshop.dao.USerService;
import com.zjc.yohoshop.di.compont.DaggerGoodsValuesFragmentCompont;
import com.zjc.yohoshop.di.modules.GoodsValuesFragmentModules;
import com.zjc.yohoshop.eventbus_bean.BaseEventBusEntity;
import com.zjc.yohoshop.eventbus_bean.GVToHABus;
import com.zjc.yohoshop.mvc.view.FragmentActivity;
import com.zjc.yohoshop.mvc.view.ImageActivity;
import com.zjc.yohoshop.mvc.view.OnLineServiceActivity;
import com.zjc.yohoshop.mvp.contract.GoodsValuesFragmentContract;
import com.zjc.yohoshop.mvp.persenter.GoodsValuesFragmentPresenter;
import com.zjc.yohoshop.mvp.view.adapter.GoodsValuesFragmentAdapter;
import com.zjc.yohoshop.mvp.view.adapter.GoodsValuesHeadSCViewPagerAdapter;
import com.zjc.yohoshop.mvp.view.adapter.GoodsValuesHeadTopViewPagerAdapter;
import com.zjc.yohoshop.mvp.view.adapter.GoodsValuesViewPagerAdapter;
import com.zjc.yohoshop.mvp.view.custom.MyTextViewGoodsValuesBottom;
import com.zjc.yohoshop.mvp.view.custom.MyTextViewHomeOriginalPrice;
import com.zjc.yohoshop.network.API;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品详情
 */
public class GoodsValuesFragment extends BaseFragment<GoodsValuesFragmentPresenter> implements GoodsValuesFragmentContract.GoodsValuesFragmentIView, View.OnClickListener, UMShareListener, CompoundButton.OnCheckedChangeListener {

    private GoodsValuesEntity goodsEntity = null;
    private SelGoodsValuesEntity selGoodsValuesEntity = null;

    private String goods_id = "1";
    private RecyclerView rv_goods_values;
    private GoodsValuesFragmentAdapter adapter;
    private LinearLayoutManager layoutManager;
    private List<GoodsValuesEntity.ValuesBean.ImgsBean> list = new ArrayList<>();
    private LinearLayout ll_goods_values_top_bg;
    private ImageView iv_goods_values_back;
    private boolean back = false;
    private boolean touch = false;
    private ImageView iv_goods_values_share;
    private RoundedImageView iv_goods_values_more;
    private LinearLayout ll_goods_values_online_service;
    private LinearLayout ll_goods_values_brand_store;
    private LinearLayout ll_goods_values_likebutton;
    private ImageView iv_goods_values_likebutton;
    private Button btn_goods_values_addcar;
    private ImageView iv_goods_values_bottom_backtop;

    private int totalY = 0;
    private int headHeight = 0;
    /**
     * 头布局控件
     */
    private View view_head;
    private ViewPager vp_item_head_goods_values;//头布局顶部商品图片
    private LinearLayout ll_item_head_goods_values_dian;
    private List<ImageView> imageViews = new ArrayList<>();
    private GoodsValuesHeadTopViewPagerAdapter headTopViewPagerAdapter;
    private TextView tv_item_head_goods_values_goods_name;
    private TextView tv_item_head_goods_values_goods_discount_price;
    private MyTextViewHomeOriginalPrice tv_item_head_goods_values_goods_original_price;
    private LinearLayout ll_item_head_goods_values_promotion;
    private TextView tv_item_head_goods_values_goods_guarantee;
    private MyTextViewGoodsValuesBottom mtvgvb_item_head_goods_values_sdpj;
    private MyTextViewGoodsValuesBottom mtvgvb_item_head_goods_values_cjwt;
    private ViewPager vp_item_head_goods_values_sc;
    private LinearLayout ll_item_head_goods_values_look_more;
    private LinearLayout ll_item_head_goods_values_red_package;
    private LinearLayout ll_item_head_goods_values_in_store;
    private ViewPager vp_item_head_goods_values_store;
    //晒单评价
    private RoundedImageView riv_item_goods_values_sdpj_tou;//头像
    private TextView tv_item_goods_values_sdpj_name;//名称
    private TextView tv_item_goods_values_sdpj_sz;//适中
    private TextView tv_item_goods_values_sdpj_title;//详情
    private ImageView iv_item_goods_values_sdpj_path;//图

    //第三方分享
    private PopupWindow share_pop;
    //加入购物车
    private PopupWindow addcar_pop;
    private TextView tv_item_bottom_addcar_goods_size;//尺寸
    private TextView tv_item_bottom_addcar_num_number;//数量
    private int color;
    private CheckBox cb_item_bottom_addxar_size_l;
    private CheckBox cb_item_bottom_addxar_size_xl;
    private CheckBox cb_item_bottom_addxar_size_xxl;
    private CheckBox cb_item_bottom_addxar_size_xxxl;
    private int size;
    private String userid;
    private View view;
    private PopupWindow morepop;//右上方三点

    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_goods_values, null);
        return view;
    }

    @Override
    public void init(View view) {

        goods_id = getArguments().getString("goods_id");
        Cursor user_id = USerService.getInstance().query("user", null, null);
        if (user_id.moveToNext()) {
            userid = user_id.getString(user_id.getColumnIndex("user_id"));
        }

        rv_goods_values = view.findViewById(R.id.rv_goods_values);
        ll_goods_values_top_bg = view.findViewById(R.id.ll_goods_values_top_bg);
        iv_goods_values_back = view.findViewById(R.id.iv_goods_values_back);
        iv_goods_values_share = view.findViewById(R.id.iv_goods_values_share);
        iv_goods_values_more = view.findViewById(R.id.iv_goods_values_more);
        ll_goods_values_online_service = view.findViewById(R.id.ll_goods_values_online_service);
        ll_goods_values_brand_store = view.findViewById(R.id.ll_goods_values_brand_store);
        ll_goods_values_likebutton = view.findViewById(R.id.ll_goods_values_likebutton);
        iv_goods_values_likebutton = view.findViewById(R.id.iv_goods_values_likebutton);
        Cursor cursor = USerService.getInstance().query("collection", "goodsid=?", new String[]{goods_id});
        if (cursor.moveToNext()){
            iv_goods_values_likebutton.setImageResource(R.drawable.likebutton_s);
        }
        btn_goods_values_addcar = view.findViewById(R.id.btn_goods_values_addcar);

        iv_goods_values_bottom_backtop = view.findViewById(R.id.iv_goods_values_bottom_backtop);

        //主RecyclerView的配置
        adapter = new GoodsValuesFragmentAdapter(list);
        layoutManager = new LinearLayoutManager(getContext());
        rv_goods_values.setLayoutManager(layoutManager);

        //获取头布局
        view_head = View.inflate(getContext(), R.layout.item_head_goods_values, null);
        ll_item_head_goods_values_dian = view_head.findViewById(R.id.ll_item_head_goods_values_dian);
        vp_item_head_goods_values = view_head.findViewById(R.id.vp_item_head_goods_values);
        tv_item_head_goods_values_goods_name = view_head.findViewById(R.id.tv_item_head_goods_values_goods_name);
        tv_item_head_goods_values_goods_discount_price = view_head.findViewById(R.id.tv_item_head_goods_values_goods_discount_price);
        tv_item_head_goods_values_goods_original_price = view_head.findViewById(R.id.tv_item_head_goods_values_goods_original_price);
        ll_item_head_goods_values_promotion = view_head.findViewById(R.id.ll_item_head_goods_values_promotion);
        tv_item_head_goods_values_goods_guarantee = view_head.findViewById(R.id.tv_item_head_goods_values_goods_guarantee);
        mtvgvb_item_head_goods_values_sdpj = view_head.findViewById(R.id.mtvgvb_item_head_goods_values_sdpj);
        mtvgvb_item_head_goods_values_cjwt = view_head.findViewById(R.id.mtvgvb_item_head_goods_values_cjwt);
        vp_item_head_goods_values_sc = view_head.findViewById(R.id.vp_item_head_goods_values_sc);
        View view_sdpj = View.inflate(getContext(), R.layout.item_goods_values_sdpj, null);
        iv_item_goods_values_sdpj_path = view_sdpj.findViewById(R.id.iv_item_goods_values_sdpj_path);
        riv_item_goods_values_sdpj_tou = view_sdpj.findViewById(R.id.riv_item_goods_values_sdpj_tou);
        tv_item_goods_values_sdpj_name = view_sdpj.findViewById(R.id.tv_item_goods_values_sdpj_name);
        tv_item_goods_values_sdpj_sz = view_sdpj.findViewById(R.id.tv_item_goods_values_sdpj_sz);
        tv_item_goods_values_sdpj_title = view_sdpj.findViewById(R.id.tv_item_goods_values_sdpj_title);
        View view_cjwt = View.inflate(getContext(), R.layout.item_goods_values_cjwt, null);
        List<View> views = new ArrayList<>();
        views.add(view_sdpj);
        views.add(view_cjwt);
        vp_item_head_goods_values_sc.setAdapter(new GoodsValuesHeadSCViewPagerAdapter(views));
        ll_item_head_goods_values_look_more = view_head.findViewById(R.id.ll_item_head_goods_values_look_more);
        ll_item_head_goods_values_red_package = view_head.findViewById(R.id.ll_item_head_goods_values_red_package);
        ll_item_head_goods_values_in_store = view_head.findViewById(R.id.ll_item_head_goods_values_in_store);
        vp_item_head_goods_values_store = view_head.findViewById(R.id.vp_item_head_goods_values_store);

        //测量头布局高度
        view_head.measure(0, 0);
        //获取measure高度
        headHeight = view_head.getMeasuredHeight() - 50;

        //添加头布局
        adapter.addHeaderView(view_head, 0);
        rv_goods_values.setAdapter(adapter);

    }

    @Override
    public void initData() {
        p.request(goods_id);


    }

    @Override
    public void setupDagger() {
        DaggerGoodsValuesFragmentCompont.builder().goodsValuesFragmentModules(new GoodsValuesFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        view.findViewById(R.id.iv_goods_values_more).setOnClickListener(this);
        rv_goods_values.addOnScrollListener(new goodsRecyclerListener());

        //滑动监听滑动到最后一页时头布局隐藏
        vp_item_head_goods_values.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                if (i == headTopViewPagerAdapter.getCount() - 1) {
                    view_head.setVisibility(View.GONE);
                    back = !back;
                }
                for (int j = 0; j < imageViews.size(); j++){
                    imageViews.get(j).setImageResource(R.drawable.tt_preview_dot_down);
                }

                if (i < imageViews.size()){
                    imageViews.get(i).setImageResource(R.drawable.icon_dot);
                }else{
                    imageViews.get(imageViews.size() - 1).setImageResource(R.drawable.icon_dot);
                }
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
        //顶部滑动图片点击事件
        vp_item_head_goods_values.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        touch = true;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touch = false;
                        break;
                    case MotionEvent.ACTION_UP:
                        if (touch){
                            ArrayList<String> images = new ArrayList<>();
                            for (GoodsValuesEntity.RecommendGoodsBean recommendGoodsBean : headTopViewPagerAdapter.getList()){
                                images.add(recommendGoodsBean.goods_img_path);
                            }
                            Intent intent = new Intent(getActivity(), ImageActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putStringArrayList("images",  images);
                            intent.putExtras(bundle);
                            getActivity().startActivity(intent);
                        }
                        break;
                }
                return false;
            }
        });
        //下方图片点击事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                if (position > 2){
                    ArrayList<String> images = new ArrayList<>();
                    for (GoodsValuesEntity.ValuesBean.ImgsBean imgsBean : adapter.getData()){
                        images.add(imgsBean.goods_img_path);
                    }
                    Intent intent = new Intent(getActivity(), ImageActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList("images",  images);
                    intent.putExtras(bundle);
                    getActivity().startActivity(intent);
                }
            }
        });


        //返回点击事件
        iv_goods_values_back.setOnClickListener(this);
        //晒单评价/常见问题ViewPager滑动监听
        vp_item_head_goods_values_sc.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

                if (i == 0) {
                    mtvgvb_item_head_goods_values_sdpj.setIsSelect(true);
                    mtvgvb_item_head_goods_values_cjwt.setIsSelect(false);
                }
                if (i == 1) {
                    mtvgvb_item_head_goods_values_cjwt.setIsSelect(true);
                    mtvgvb_item_head_goods_values_sdpj.setIsSelect(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        btn_goods_values_addcar.setOnClickListener(this);//加入购物车
        mtvgvb_item_head_goods_values_sdpj.setIsSelect(true);
        mtvgvb_item_head_goods_values_sdpj.setOnClickListener(this);
        mtvgvb_item_head_goods_values_cjwt.setOnClickListener(this);
        iv_goods_values_bottom_backtop.setOnClickListener(this);//置顶
        view.findViewById(R.id.iv_goods_values_bottom_car).setOnClickListener(this);//购物车
        iv_goods_values_share.setOnClickListener(this);//分享
        ll_goods_values_likebutton.setOnClickListener(this);//收藏
        ll_goods_values_online_service.setOnClickListener(this);//在线客服
        ll_goods_values_brand_store.setOnClickListener(this);


    }


    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override//商品详情返回接口
    public void GoodsValuesSuccess(GoodsValuesEntity entity) {
        goodsEntity = entity;

        headTopViewPagerAdapter = new GoodsValuesHeadTopViewPagerAdapter(entity.recommend_goods);
        vp_item_head_goods_values.setAdapter(headTopViewPagerAdapter);
        for (int i = 0; i < entity.recommend_goods.size() - 1; i++){
            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(R.drawable.tt_preview_dot_down);
            imageView.setPadding(10, 10, 10, 10);
            imageViews.add(imageView);
            ll_item_head_goods_values_dian.addView(imageView);
        }

        tv_item_head_goods_values_goods_name.setText(entity.values.get(0).goods_name);
        tv_item_head_goods_values_goods_discount_price.setText("￥" + entity.values.get(0).goods_discount_price);
        tv_item_head_goods_values_goods_original_price.setText("￥" + entity.values.get(0).goods_original_price);
        tv_item_head_goods_values_goods_guarantee.setText(entity.values.get(0).goods_guarantee);
//        vp_item_head_goods_values_sc = view_head.findViewById(R.id.vp_item_head_goods_values_sc);
//        ll_item_head_goods_values_look_more = view_head.findViewById(R.id.ll_item_head_goods_values_look_more);
//        ll_item_head_goods_values_red_package = view_head.findViewById(R.id.ll_item_head_goods_values_red_package);
//        ll_item_head_goods_values_in_store = view_head.findViewById(R.id.ll_item_head_goods_values_in_store);
        vp_item_head_goods_values_store.setAdapter(new GoodsValuesViewPagerAdapter(entity.recommend_goods));

        entity.values.get(0).imgs.get(0).itemType = 1;
        entity.values.get(0).imgs.get(1).itemType = 2;
        adapter.setNewData(entity.values.get(0).imgs);


        Glide.with(getContext()).load(API.BaseUrl + entity.values.get(0).imgs.get(0).goods_img_path).into(riv_item_goods_values_sdpj_tou);
        Glide.with(getContext()).load(API.BaseUrl + entity.values.get(0).imgs.get(1).goods_img_path).into(iv_item_goods_values_sdpj_path);
        tv_item_goods_values_sdpj_name.setText(entity.values.get(0).goods_name);
        tv_item_goods_values_sdpj_sz.setText("适中");
        tv_item_goods_values_sdpj_title.setText(entity.values.get(0).goods_guarantee);

    }

    @Override//尺寸返回接口
    public void SelGoodsValuesSuccess(SelGoodsValuesEntity entity) {
        selGoodsValuesEntity = entity;
    }

    @Override//加入购物车返回接口
    public void AddCarSuccess(AddCarEntity entity) {
        Toast.makeText(getContext(), entity.msg, Toast.LENGTH_SHORT).show();
        addcar_pop.dismiss();
    }


    @Override //收藏商品返回接口
    public void CollectionSuccess(CollectionEntity entity) {
        if ("1".equals(entity.statues)){
            ContentValues values_like = new ContentValues();
            values_like.put("goodsid", goods_id);
            values_like.put("goods_img", goodsEntity.values.get(0).imgs.get(0).goods_img_path);
            values_like.put("goods_name", goodsEntity.values.get(0).goods_name);
            values_like.put("goods_price", goodsEntity.values.get(0).goods_discount_price);
            long collection = USerService.getInstance().insert("collection", values_like);
            if (collection > 0){
                iv_goods_values_likebutton.setImageResource(R.drawable.likebutton_s);
            }
        }
    }

    @Override //取消收藏返回接口
    public void UncollectionSuccess(UncollectionEntity entity) {
        if ("1".equals(entity.statues)){
            int collection = USerService.getInstance().delete("collection", "goodsid=?", new String[]{goods_id});
            if (collection > 0) {
                iv_goods_values_likebutton.setImageResource(R.drawable.likebutton_p);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_goods_values_back://返回按钮 当back为真是表示当前在商品信息不符 为假时表示返回上一页面
                if (back) {
                    view_head.setVisibility(View.VISIBLE);
                    back = !back;
                } else {
                    getActivity().finish();
                }
                break;
            case R.id.iv_goods_values_share://打开底部第三方分享窗口
                share_popupwindow();
                break;
            case R.id.iv_goods_values_more://右上方三点
                more_popupwindow();
                break;
            case R.id.ll_item_goods_values_more_xx://消息
                Toast.makeText(getContext(), "消息", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_goods_values_more_sy://首页
                EventBus.getDefault().post(new GVToHABus(BaseEventBusEntity.GVTOHA, "首页"));
                getActivity().finish();
                break;
            case R.id.ll_item_goods_values_more_wd://我的
                EventBus.getDefault().post(new GVToHABus(BaseEventBusEntity.GVTOHA, "我的"));
                getActivity().finish();
                break;
            case R.id.ll_item_goods_values_more_fl://分类
                EventBus.getDefault().post(new GVToHABus(BaseEventBusEntity.GVTOHA, "分类"));
                getActivity().finish();
                break;
            case R.id.ll_item_goods_values_more_lljl://浏览记录
                Toast.makeText(getContext(), "浏览记录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_goods_values_online_service://在线客服
                Intent onLineService = new Intent(getActivity(), OnLineServiceActivity.class);
                getActivity().startActivity(onLineService);
                break;
            case R.id.ll_goods_values_likebutton://收藏
                Cursor likeGoods = USerService.getInstance().query("collection", "goodsid=?", new String[]{goods_id});
                if (likeGoods.moveToNext()){
                    p.UncollectionRequest(userid, goods_id);
                }else{
                    p.CollectionRequest(userid, goods_id);
                }
                break;
            case R.id.ll_goods_values_brand_store://品牌店铺
                Intent shop_details = new Intent(getActivity(), FragmentActivity.class);
                shop_details.putExtra("type", "shop_details");
                getActivity().startActivity(shop_details);
                break;
            case R.id.mtvgvb_item_head_goods_values_sdpj://晒单评价
                mtvgvb_item_head_goods_values_cjwt.setIsSelect(false);
                mtvgvb_item_head_goods_values_sdpj.setIsSelect(true);
                vp_item_head_goods_values_sc.setCurrentItem(0);
                break;
            case R.id.mtvgvb_item_head_goods_values_cjwt://常见问题
                mtvgvb_item_head_goods_values_cjwt.setIsSelect(true);
                mtvgvb_item_head_goods_values_sdpj.setIsSelect(false);
                vp_item_head_goods_values_sc.setCurrentItem(1);
                break;
            case R.id.iv_goods_values_bottom_backtop://返回顶部
                totalY = 0;
                iv_goods_values_bottom_backtop.setVisibility(View.GONE);
                rv_goods_values.scrollToPosition(0);
                break;
            case R.id.iv_goods_values_bottom_car://购物车
                Intent goods_car = new Intent(getActivity(), FragmentActivity.class);
                goods_car.putExtra("type", "goods_car");
                getActivity().startActivity(goods_car);
                break;
            case R.id.btn_goods_values_addcar://打开底部加入购物车窗口
                addcar_popupwindow();
                break;
            case R.id.iv_item_bottom_share_goods_share_wx://微信好友分享
                break;
            case R.id.iv_item_bottom_share_goods_share_qq://QQ好友分享
                break;
            case R.id.iv_item_bottom_share_goods_share_wpq://微信朋友圈分享
                break;
            case R.id.iv_item_bottom_share_goods_share_qqkj://QQ空间
                break;
            case R.id.iv_item_bottom_share_goods_share_xl://新浪微博分享
                if (share_pop != null) {
                    share_pop.dismiss();
                }
                sina_share();
                break;
            case R.id.iv_item_bottom_share_goods_share_qr://生成海报
                break;
            case R.id.tv_item_bottom_share_cancel://取消分享
                if (share_pop != null) {
                    share_pop.dismiss();
                }
                break;
            case R.id.iv_item_bottomaddcar_close://取消加入购物车
                addcar_pop.dismiss();
                break;
            case R.id.tv_item_bottom_addcar_num_subtract://减少数量
                int subtract = Integer.parseInt(tv_item_bottom_addcar_num_number.getText().toString().trim());
                if (subtract > 1) {
                    subtract--;
                }
                tv_item_bottom_addcar_num_number.setText("" + subtract);
                break;
            case R.id.tv_item_bottom_addcar_num_add://增加数量
                int add = Integer.parseInt(tv_item_bottom_addcar_num_number.getText().toString().trim());
                add++;
                tv_item_bottom_addcar_num_number.setText("" + add);
                break;
            case R.id.btn_item_bottom_addcar://加入购物车
                if (size >= selGoodsValuesEntity.size.size()) {
                    Toast.makeText(getContext(), "请选择尺寸", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(userid)) {
                    p.addCarRequest(userid, goods_id
                            , goodsEntity.values.get(0).goods_name
                            , selGoodsValuesEntity.colors.get(color).colorvalues
                            , selGoodsValuesEntity.size.get(size).size_name
                            , tv_item_bottom_addcar_num_number.getText().toString().trim()
                            , goodsEntity.values.get(0).goods_discount_price);
                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    //多选按钮组监听
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_item_bottom_addxar_size_L:
                if (isChecked) {
                    size = 0;
                    cb_item_bottom_addxar_size_xl.setChecked(false);
                    cb_item_bottom_addxar_size_xxl.setChecked(false);
                    cb_item_bottom_addxar_size_xxxl.setChecked(false);
                }
                break;
            case R.id.cb_item_bottom_addxar_size_XL:
                if (isChecked) {
                    size = 1;
                    cb_item_bottom_addxar_size_l.setChecked(false);
                    cb_item_bottom_addxar_size_xxl.setChecked(false);
                    cb_item_bottom_addxar_size_xxxl.setChecked(false);
                }
                break;
            case R.id.cb_item_bottom_addxar_size_XXL:
                if (isChecked) {
                    size = 2;
                    cb_item_bottom_addxar_size_l.setChecked(false);
                    cb_item_bottom_addxar_size_xl.setChecked(false);
                    cb_item_bottom_addxar_size_xxxl.setChecked(false);
                }
                break;
            case R.id.cb_item_bottom_addxar_size_XXXL:
                if (isChecked) {
                    size = 3;
                    cb_item_bottom_addxar_size_l.setChecked(false);
                    cb_item_bottom_addxar_size_xl.setChecked(false);
                    cb_item_bottom_addxar_size_xxl.setChecked(false);
                }
                break;
        }
        if (!cb_item_bottom_addxar_size_l.isChecked() && !cb_item_bottom_addxar_size_xl.isChecked() && !cb_item_bottom_addxar_size_xxl.isChecked() && !cb_item_bottom_addxar_size_xxxl.isChecked()) {
            tv_item_bottom_addcar_goods_size.setText("");
            size = selGoodsValuesEntity.size.size();
        } else {
            tv_item_bottom_addcar_goods_size.setText("、 " + selGoodsValuesEntity.size.get(size).size_name);
        }
    }

    //加入购物车底部弹出窗体
    public void addcar_popupwindow() {
        //加入购物车控件
        //TODO 1:实例化对象
        addcar_pop = new PopupWindow(getActivity());
        //TODO 2：设置属性
        View view_addcar = LayoutInflater.from(getActivity()).inflate(R.layout.item_bottom_addcar, null);
        //初始化控件
        ImageView iv_item_bottom_addcar_img = view_addcar.findViewById(R.id.iv_item_bottom_addcar_img);
        TextView tv_item_bottom_addcar_goods_discount_price = view_addcar.findViewById(R.id.tv_item_bottom_addcar_goods_discount_price);
        MyTextViewHomeOriginalPrice tv_item_bottom_addcar_goods_original_price = view_addcar.findViewById(R.id.tv_item_bottom_addcar_goods_original_price);
        TextView tv_item_bottom_addcar_goods_color = view_addcar.findViewById(R.id.tv_item_bottom_addcar_goods_color);
        tv_item_bottom_addcar_goods_size = view_addcar.findViewById(R.id.tv_item_bottom_addcar_goods_size);
        TextView tv_item_bottom_addcar_goods_color_select = view_addcar.findViewById(R.id.tv_item_bottom_addcar_goods_color_select);
        tv_item_bottom_addcar_num_number = view_addcar.findViewById(R.id.tv_item_bottom_addcar_num_number);
        cb_item_bottom_addxar_size_l = view_addcar.findViewById(R.id.cb_item_bottom_addxar_size_L);//L
        cb_item_bottom_addxar_size_xl = view_addcar.findViewById(R.id.cb_item_bottom_addxar_size_XL);//XL
        cb_item_bottom_addxar_size_xxl = view_addcar.findViewById(R.id.cb_item_bottom_addxar_size_XXL);//XXL
        cb_item_bottom_addxar_size_xxxl = view_addcar.findViewById(R.id.cb_item_bottom_addxar_size_XXXL);//XXXL

        //添加点击监听
        view_addcar.findViewById(R.id.iv_item_bottomaddcar_close).setOnClickListener(this);//关闭键
        view_addcar.findViewById(R.id.tv_item_bottom_addcar_num_subtract).setOnClickListener(this);//减按钮
        view_addcar.findViewById(R.id.tv_item_bottom_addcar_num_add).setOnClickListener(this);//增加按钮
        view_addcar.findViewById(R.id.btn_item_bottom_addcar).setOnClickListener(this);//加入按钮+
        //添加多选框监听
        cb_item_bottom_addxar_size_l.setOnCheckedChangeListener(this);//L
        cb_item_bottom_addxar_size_xl.setOnCheckedChangeListener(this);//XL
        cb_item_bottom_addxar_size_xxl.setOnCheckedChangeListener(this);//XXL
        cb_item_bottom_addxar_size_xxxl.setOnCheckedChangeListener(this);//XXXL


        //控件赋值
        //图片
        Glide.with(getContext()).load(API.BaseUrl + goodsEntity.values.get(0).imgs.get(0).goods_img_path).into(iv_item_bottom_addcar_img);
        //价钱
        tv_item_bottom_addcar_goods_discount_price.setText("￥" + goodsEntity.values.get(0).goods_discount_price);
        tv_item_bottom_addcar_goods_original_price.setText("￥" + goodsEntity.values.get(0).goods_original_price);
        //颜色
        color = (int) (Math.random() * (selGoodsValuesEntity.colors.size() - 1));
        tv_item_bottom_addcar_goods_color.setText("已选 : " + selGoodsValuesEntity.colors.get(color).colorvalues);
        tv_item_bottom_addcar_goods_color_select.setText(selGoodsValuesEntity.colors.get(color).colorvalues);


        addcar_pop.setContentView(view_addcar);
        addcar_pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        addcar_pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置点击外部消失
        addcar_pop.setOutsideTouchable(true);
        //TODO 3:展示
        /**
         * @param parent 父布局
         * @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
         * @param x x轴偏移量
         * @param y y轴偏移量
         */
        View parent = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_goods_values, null);
        addcar_pop.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    //第三方分享底部弹出窗体
    public void share_popupwindow() {
        //TODO 1:实例化对象
        share_pop = new PopupWindow(getActivity());
        //TODO 2：设置属性
        View view_addcar = LayoutInflater.from(getActivity()).inflate(R.layout.item_bottom_share, null);
        //添加点击监听
        view_addcar.findViewById(R.id.iv_item_bottom_share_goods_share_wx).setOnClickListener(this);
        view_addcar.findViewById(R.id.iv_item_bottom_share_goods_share_qq).setOnClickListener(this);
        view_addcar.findViewById(R.id.iv_item_bottom_share_goods_share_wpq).setOnClickListener(this);
        view_addcar.findViewById(R.id.iv_item_bottom_share_goods_share_qqkj).setOnClickListener(this);
        view_addcar.findViewById(R.id.iv_item_bottom_share_goods_share_xl).setOnClickListener(this);
        view_addcar.findViewById(R.id.iv_item_bottom_share_goods_share_qr).setOnClickListener(this);
        view_addcar.findViewById(R.id.tv_item_bottom_share_cancel).setOnClickListener(this);

        share_pop.setContentView(view_addcar);
        share_pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        share_pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置点击外部消失
        share_pop.setOutsideTouchable(true);
        //TODO 3:展示
        /**
         * @param parent 父布局
         * @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
         * @param x x轴偏移量
         * @param y y轴偏移量
         */
        View parent = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_goods_values, null);
        share_pop.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    //新浪微博三方分享方法
    private void sina_share() {
        new ShareAction(getActivity())
                .setPlatform(SHARE_MEDIA.SINA)
                .withText(goodsEntity.values.get(0).goods_name)
                .setCallback(this)
                .share();
    }
    //右上方三点
    public void more_popupwindow(){
        //TODO 1:实例化对象
        morepop = new PopupWindow(getContext());
        //TODO 2：设置属性
        View view_more=View.inflate(getContext(), R.layout.item_goods_values_more_menu, null);
        view_more.findViewById(R.id.ll_item_goods_values_more_xx).setOnClickListener(this);
        view_more.findViewById(R.id.ll_item_goods_values_more_sy).setOnClickListener(this);
        view_more.findViewById(R.id.ll_item_goods_values_more_wd).setOnClickListener(this);
        view_more.findViewById(R.id.ll_item_goods_values_more_fl).setOnClickListener(this);
        view_more.findViewById(R.id.ll_item_goods_values_more_lljl).setOnClickListener(this);
        morepop.setContentView(view_more);
        morepop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        morepop.setWidth(500);
        //设置点击外部消失
        morepop.setOutsideTouchable(true);
        morepop.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        //设置透明的颜色背景。以前的那个方法过时了用这个，与下面一个意思就是分开不开写的区别，在这可以设置背景色，也可以在布局里设置背景色
//        setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        //TODO 3:展示

//        popupWindow.showAtLocation(iv_goods_values_more, Gravity.BOTTOM,0,0);
        morepop.showAsDropDown(iv_goods_values_more, 0, 0);
    }

    //RecyclerView滑动监听器
    private class goodsRecyclerListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            totalY += dy;
            float alpah = (float) totalY / (float) 400;
            //alpha 0-1;
            if (alpah > 0.9) {
                alpah = 0.9f;
            }
            if (alpah <= 0) {
                alpah = 0;
            }
            ll_goods_values_top_bg.setAlpha(alpah);

            if (totalY >= headHeight && !back) {
                iv_goods_values_bottom_backtop.setVisibility(View.VISIBLE);
            } else {
                iv_goods_values_bottom_backtop.setVisibility(View.GONE);
            }

        }
    }



    /**
     * 以下4个方法是友盟分享监听
     *
     * @param share_media
     */
    @Override//开始
    public void onStart(SHARE_MEDIA share_media) {
        Log.e("ZJC", "商品详情 -> 新浪微博 -> 分享 -> 开始");
    }

    @Override//结果
    public void onResult(SHARE_MEDIA share_media) {
        if (share_media == SHARE_MEDIA.SINA) {
            Log.e("ZJC", "商品详情 -> 新浪微博 -> 分享 -> 结果 -> " + share_media.toString());
        }
        Toast.makeText(getContext(), "分享成功", Toast.LENGTH_SHORT).show();
        share_pop.dismiss();
    }

    @Override//报错
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        Log.e("ZJC", "商品详情 -> 新浪微博 -> 分享 -> 错误信息 -> " + throwable.toString());
    }

    @Override//取消
    public void onCancel(SHARE_MEDIA share_media) {
        Log.e("ZJC", "商品详情 -> 新浪微博 -> 分享 -> 取消");
    }

}
